package com.woniuxy.microgoodsserver.attributemag.mapper;

import com.woniuxy.microcommonserver.bean.gy.AttributeInfo;
import com.woniuxy.microcommonserver.bean.gy.ValueInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
    规格数据操作层
 * @params:
 * @return
 */
@Mapper
public interface AttributeMapper {

    /**
     增添规格
     * @params:
     * @return
     */
    @Insert("insert into attribute_info(attribute_name,file_name,sort,remark,type,status,good_category_info2_id) " +
            "values(#{a.attributeName},#{a.fileName},#{a.sort},#{a.remark},#{a.type},0,#{SortId})")
    public void addAttribute(@Param("a") AttributeInfo attributeInfo, @Param("SortId") Integer SortId);
    /**
        根据备注和规格名称查询唯一ID
     * @return
     */
    @ResultType(Integer.class)
    @Select("select id from attribute_info where attribute_name=#{a.attributeName} and remark = #{a.remark} and status=0")
    public Integer findAttributeId(@Param("a") AttributeInfo attributeInfo);

    /**添加属性值

     * @params:
     * @return
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "v.id"

    )
    @Insert("insert into value_info(value,sort,status,attribute_info_id) " +
            "values(#{v.value},#{v.sort},0,#{id})")
    public void addValues(@Param("v") ValueInfo valueInfo,@Param("id") int id);


    /**
        查询所有的规格
     * @params:
     * @return
     */
    @ResultType(AttributeInfo.class)
    @Select("select id,attribute_name attributeName,remark,sort from attribute_info where status=0")
    public List<AttributeInfo> findAllAttribute();


    /**
        删除规格（逻辑删除）
     * @params:
     * @return
     */
    @Update("update attribute_info set status=1 where id = #{a.id}")
    public void deleteAttribute(@Param("a") AttributeInfo attributeInfo);

    /**
       根据ID查询规格和其属性
     * @params:
     * @return
     */
    @ResultType(Map.class)
    @Select("select  ai.id,ai.attribute_name,ai.file_name,ai.sort,ai.remark," +
            "ai.type,vi.id vid,vi.value,vi.sort from attribute_info ai,value_info vi " +
            "where ai.id = #{a.id} and vi.attribute_info_id = ai.id and ai.status=0 and vi.status=0")
    public List<Map> findAttributeById(@Param("a") AttributeInfo attributeInfo);
    /**
        根据属性值ID删除
     * @params:
     * @return
     */
    @Update("update value_info set status=1 where id = #{vid}")
    public void deleteValue(Integer vid);


    /**
        根据ID查询Value
     * @params:
     * @return
     */
    @ResultType(ValueInfo.class)
    @Select("select id from value_info where value = #{s}")
    public ValueInfo findValueById(@Param("s") String s);

    /**修改规格的信息

     * @params:
     * @return
     */

    public void updateAttribute(@Param("a") AttributeInfo attributeInfo, @Param("gid") Integer goodCategoryInfo2Id);


    /**
     根据分类ID查询规格名称和其规格属性值
     * @params:
     * @return
     */

    public List<Map> findAttributeAndValue(Integer sortId);
}
